package com.xjscrm.console.chat.utils;

import com.xjscrm.console.chat.frame.dto.ChatRecordDTO;
import com.xjscrm.console.chat.frame.dto.ChatSessionDTO;
import com.xjscrm.console.chat.frame.send.SNewSessionMsg;
import com.xjscrm.console.chat.frame.send.SResultMsg;
import com.xjscrm.console.entity.ChatTalkRecord;
import com.xjscrm.console.entity.ChatTalkSession;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.beans.BeanUtils;

import java.util.Objects;

/**
 * @author mingyang.chen create in 2024/3/21 18:02
 */
public class MessageUtil {

    @Deprecated
    public static void sendNewSessionTalk(Channel channel, ChatTalkSession session, ChatTalkRecord record) {
        ChatSessionDTO chatSessionDTO = new ChatSessionDTO();
        BeanUtils.copyProperties(session, chatSessionDTO);

        ChatRecordDTO chatRecordDTO = new ChatRecordDTO();
        if (Objects.nonNull(record)) {
            BeanUtils.copyProperties(record, chatRecordDTO);
            chatRecordDTO.parse();
        }

        SNewSessionMsg newSessionMsg = new SNewSessionMsg();

        newSessionMsg.setDeviceUniqueId(session.getDeviceUniqueId());
        newSessionMsg.setMerId(session.getMerId());
        newSessionMsg.setSession(chatSessionDTO);
        newSessionMsg.setTalkRecord(chatRecordDTO);
        newSessionMsg.setSuccess(true);

        channel.writeAndFlush(new TextWebSocketFrame(JsonUtils.toJson(newSessionMsg)));
    }

    public static void sendNewSessionTalk(Channel channel, ChatSessionDTO session, ChatRecordDTO record) {

        SNewSessionMsg newSessionMsg = new SNewSessionMsg();

        newSessionMsg.setDeviceUniqueId(session.getDeviceUniqueId());
        newSessionMsg.setMerId(session.getMerId());
        newSessionMsg.setSession(session);
        newSessionMsg.setTalkRecord(record);
        newSessionMsg.setSuccess(true);

        channel.writeAndFlush(new TextWebSocketFrame(JsonUtils.toJson(newSessionMsg)));
    }

    public static void sendMsg(Channel channel, SResultMsg sResultMsg) {
        channel.writeAndFlush(new TextWebSocketFrame(JsonUtils.toJson(sResultMsg)));
    }
}
